package com.chengyi.serialport.demo

import android.app.Application
import android.content.Context
import androidx.core.content.ContextCompat
import androidx.multidex.MultiDex
import com.bumptech.glide.Glide
import com.chengyi.serialport.demo.gson.BundleTypeAdapterFactory
import com.chengyi.serialport.demo.utils.PictureSelectorHelp
import com.chengyi.serialport.demo.view.BezierCircleHeader
import com.hjq.gson.factory.GsonFactory
import com.luck.picture.lib.app.IApp
import com.luck.picture.lib.app.PictureAppMaster
import com.luck.picture.lib.engine.PictureSelectorEngine
import com.print.usbprint.util.USBUtil
import com.scwang.smart.refresh.footer.BallPulseFooter
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.taobao.accs.utl.UtilityImpl
import com.wyj.utils.AppConfig
import com.wyj.utils.MApplication
import me.jessyan.autosize.utils.AutoSizeUtils
import me.zhouzhuo.zzimagebox.ZzImageBox
import okhttp3.OkHttpClient
import org.greenrobot.eventbus.EventBus
import rxhttp.RxHttpPlugins
import rxhttp.wrapper.converter.GsonConverter
import java.util.concurrent.TimeUnit


class MyApplication : Application(), IApp {
    override fun onCreate() {
        super.onCreate()
        startTime = System.currentTimeMillis()
        val isMainProcess = UtilityImpl.isMainProcess(this)
        GsonFactory.registerTypeAdapterFactory(BundleTypeAdapterFactory())
        MApplication.onCreate(this, isMainProcess)
        val okHttpClientBuilder = OkHttpClient.Builder().connectTimeout(60000L, TimeUnit.MILLISECONDS).readTimeout(60000L, TimeUnit.MILLISECONDS)
        RxHttpPlugins.init(okHttpClientBuilder.build()).setConverter(GsonConverter.create(GsonFactory.getSingletonGson())).setOnParamAssembly {
            it.addHeader("version", AppConfig.DEFAULT.VERSION)
            it.addHeader("RefererSource", "android")
        }
        PictureAppMaster.getInstance().app = this;
        if (isMainProcess) {
            ZzImageBox.setGlobalOnLineImageLoader { context, iv, url, imgSize, placeHolder ->
                Glide.with(context).load(url).override(imgSize, imgSize).placeholder(placeHolder).into(iv)
            }
        }
        //杰普维打印机初始化
        USBUtil.getInstance().init(this)
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    companion object {
        @JvmStatic
        val application: MyApplication
            get() = MApplication.application as MyApplication

        @JvmStatic
        var startTime: Long = 0

        @JvmStatic
        fun exitApplication() {
            EventBus.getDefault().post(AppConfig.EVENT_CODE.EXIT_APPLICATION())
        }

        init {
            SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, _ ->
                BezierCircleHeader(context).also {
                    it.minimumHeight = AutoSizeUtils.dp2px(context, 72f)
                    it.setPrimaryColors(ContextCompat.getColor(context, R.color.qmui_config_color_white), ContextCompat.getColor(context, R.color.qmui_config_color_blue))
                }
            }
            SmartRefreshLayout.setDefaultRefreshFooterCreator { context, _ ->
                BallPulseFooter(context).also {
                    it.minimumHeight = AutoSizeUtils.dp2px(context, 72f)
                    it.setPrimaryColors(ContextCompat.getColor(context, R.color.qmui_config_color_blue), ContextCompat.getColor(context, R.color.qmui_config_color_white))
                }
            }
        }
    }

    override fun getAppContext(): Context = this

    override fun getPictureSelectorEngine(): PictureSelectorEngine = PictureSelectorHelp.PictureSelectorEngineImp()
}